
/**
 * Content: ThreadLocal使用
 * --------------------------------------------------
 * ThreadLocal的使用: https://blog.csdn.net/upstream480/article/details/120310476
 */

public class java_1122_class_ThreadLocal {

    public static ThreadLocal<String> local = new ThreadLocal<>();

    public static void main(String[] args) {

        // ThreadLocal使用
        System.out.println("--------------------ThreadLocal使用--------------------");

        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                local.set("thread-1 local value");
                try {
                    // 等待5000毫秒，确保threadTwo 执行remove完成
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(local.get());
            }
        });

        Thread threadTwo = new Thread(new Runnable() {
            @Override
            public void run() {
                local.set("thread-2 local value");
                System.out.println(local.get());
                local.remove();
                System.out.println("thread-2 remove 变量local 操作完毕。");
            }
        });

        threadOne.start();
        threadTwo.start();

    }
}

